package com.zbf.dao;

import com.zbf.entity.MyUser;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.test.context.transaction.AfterTransaction;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/6/25  10:24
 * 描述:
 */
public interface MyUserDao2 extends JpaRepository<MyUser,Long> {

    /**
     * 作者: LCG
     * 日期: 2021/6/25  10:32
     * 描述: 使用Query查询，以及参数的传递方式使用 ？占位符
     * @Param [password, userName]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    @Query(value = "select * from jpa_user where user_name like CONCAT('%',?2,'%') and password=?1",nativeQuery = true)
    public List<Map<String,Object>> getUserList(String password,String userName);


    /**
     * 作者: LCG
     * 日期: 2021/6/25  10:34
     * 描述: Query的分页查询
     * @Param [userName, pageable]
     * @Return org.springframework.data.domain.Page<java.util.Map<java.lang.String,java.lang.Object>>
     */
    @Query(value = "select * from jpa_user where user_name like CONCAT('%',?1,'%')",nativeQuery = true)
    public Page<Map<String,Object>> getUserPage(String userName, Pageable pageable);


    /**
     * 作者: LCG
     * 日期: 2021/6/25  10:35
     * 描述: Query的传递参数的第二种方案
     * @Param [password, userName]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    @Query(value = "select * from jpa_user where user_name like CONCAT('%',:userName,'%') and password=:password",nativeQuery = true)
    public List<Map<String,Object>> getUserList2(@Param("password") String password,@Param("userName") String userName);



    /**
     * 作者: LCG
     * 日期: 2021/6/25  10:37
     * 描述: 更新的操作
     * @Param []
     * @Return int
     */
    @Query(value = "update jpa_user set user_name=:userName where id=:id",nativeQuery = true)
    @Modifying
    //开启事务---执行 insert update delete的时候要添加事务注解
    @Transactional
    public int updateUserInfo(@Param("userName") String userName,@Param("id") Long id);

    
    /**
     * 作者: LCG
     * 日期: 2021/6/25  10:40
     * 描述: 插入数据的操作
     * @Param [userName]
     * @Return int
     */
    @Query(value = "insert into jpa_user(user_name)values (:userName)",nativeQuery = true)
    @Modifying
    @Transactional
    public int insertUserInfo(@Param("userName") String userName);


    /**
     * 作者: LCG
     * 日期: 2021/6/25  10:42
     * 描述: 删除操作
     * @Param [id]
     * @Return int
     */
    @Query(value = "delete from jpa_user where id=:id",nativeQuery = true)
    @Modifying
    @Transactional
    public int deleteUserInfo(@Param("id") Long id);

}
